package resoft.report;

import java.io.PrintStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * <p></p>
 * User: albert
 * Date: Sep 3, 2007
 * Time: 12:49:04 AM
 */
public class PrintUtil {

    private static final byte ESCAPE = 27;



    /**
     * begin to print
     * */
    public static void startPrint() {
        byte[] bytes = new byte[4];
        bytes[0] = ESCAPE;
        bytes[1] = (byte)'[';
        bytes[2] = (byte) '5';
        bytes[3] = (byte) 'i';
        write(bytes);
    }

    /**
     * print a line
     *
     * @param line String
     * */
    public static void print(String line) {
        try {
            write(line.getBytes("GB2312"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        write(new byte[]{'\n'});
    }

    /**
     * set backPage
     * */
    public static void backPage() {
        byte[] bytes = new byte[2];
        bytes[0] = ESCAPE;
        bytes[1] = (byte)'L';
        write(bytes);
    }

    /**
     * end print
     * */
    public static void endPrint() {
        byte[] bytes = new byte[4];
        bytes[0] = ESCAPE;
        bytes[1] = (byte)'[';
        bytes[2] = (byte) '4';
        bytes[3] = (byte) 'i';
        write(bytes);
    }


    private static void write(byte[] bytes) {
        try {
            out.write(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static PrintStream out = System.out;
}
